package main.java.tmi.movement.web;

import java.sql.SQLException;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import main.java.tmi.common.service.UserVO;
import main.java.tmi.movement.service.MovementService;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;


/**
 *  Controller
 *
 * @author min
 * @since 2010. 09. 08.
 */
@Controller
public class MovementController {
	
    @Resource(name = "movementServiceImpl")
    private MovementService movementService;
    
    /**
     * The Constructor
     */
    public MovementController() {
        //logger = LogFactory.getLog(this.getClass());
    }

    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/movement")
    public String viewList(HttpServletRequest request, HttpServletResponse response,
    		@RequestParam Map param, ModelMap model) throws SQLException {
        System.out.println("====================================================");
        System.out.println("===============movement start=========================");
        System.out.println("====================================================");
        UserVO userInfo = (UserVO) RequestContextHolder.getRequestAttributes().getAttribute("userInfo", RequestAttributes.SCOPE_SESSION);
		model.addAttribute("roleId",userInfo.getRoleId());
        model.addAttribute("ordPseq",param.get("ordPseq"));
        model.addAttribute("ordNo1",param.get("ordNo1"));
        model.addAttribute("ordNo2",param.get("ordNo2"));
        model.addAttribute("clearNo",param.get("clearNo"));
        model.addAttribute("mawb",param.get("mawb"));
        model.addAttribute("hawb",param.get("hawb"));
        return "/tmi/movement/movement";
    } 
    

    /**
     * 조회
     */
    @RequestMapping("/getMovementView")
    public void getMovementView(@RequestParam Map param, Model model) throws Exception {
        System.out.println("=======================> getMovementView start");

        //저장 완료 후 조회
		model.addAttribute("resultMap",movementService.getMovementView(param));
		model.addAttribute("resultDetailList",movementService.getMovementDetailList(param));
    }
    

    /**
     * movement SAVE
     */
    @RequestMapping("/saveMovement")
    public void saveMovement(@RequestParam Map param, Model model , HttpServletRequest request) throws Exception {
    	System.out.println("=======================> saveMovement Start");
    	
    	UserVO userInfo = (UserVO) RequestContextHolder.getRequestAttributes().getAttribute("userInfo", RequestAttributes.SCOPE_SESSION);
    	param.put("sesseionId", userInfo.getUserId());
    	System.out.println("sesseionId============> "+userInfo.getUserId());
    	

       	model.addAttribute("result", movementService.modifyMovement(param , request) );

        //저장 완료 후 조회
    	model.addAttribute("resultMap",movementService.getMovementView(param));
		model.addAttribute("resultDetailList",movementService.getMovementDetailList(param));
		model.addAttribute("result",true);
    }
    
    
    /**
    * 수정 디테일
    */
    @RequestMapping("/movementDetailUpdate")
    public void movementDetailUpdate(@RequestParam Map param, Model model) throws Exception {
        System.out.println("=======================> movementDetailUpdate Start");
        

    	UserVO userInfo = (UserVO) RequestContextHolder.getRequestAttributes().getAttribute("userInfo", RequestAttributes.SCOPE_SESSION);
    	param.put("sesseionId", userInfo.getUserId());
    	System.out.println("sesseionId============> "+userInfo.getUserId());
    	
    	
        model.addAttribute("result", movementService.movementDetailUpdate(param) );
    }
    
    

    @RequestMapping("/deleteDoMovement")
    public void deleteDo(@RequestParam Map param, Model model , HttpServletRequest request) throws Exception {
    	System.out.println("=======================> deleteDoMovement Start");
    	

    	UserVO userInfo = (UserVO) RequestContextHolder.getRequestAttributes().getAttribute("userInfo", RequestAttributes.SCOPE_SESSION);
    	System.out.println(">>>>>>>>>>>>>>>"+userInfo );
    	param.put("sesseionId", userInfo.getUserId());
    	
		model.addAttribute("result",movementService.deleteDo(param));
    }
    
    
    
    /**
     * 확정
     */
     @RequestMapping("/confirmMovement")
     public void confirmMovement(@RequestParam Map param, Model model) throws Exception {
         System.out.println("=======================> confirmMovement Start");
         
     	UserVO userInfo = (UserVO) RequestContextHolder.getRequestAttributes().getAttribute("userInfo", RequestAttributes.SCOPE_SESSION);
     	param.put("sesseionId", userInfo.getUserId());
     	
         model.addAttribute("result", movementService.confirmMovement(param) );
     }
    
     
}
